<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="success" @click="addDataBtn">新增</el-button>
          <el-button type="warning" @click="deleteAll">多项删除</el-button>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <el-input placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <el-table
      style="width: 100%; margin: 10px auto"
      @selection-change="handleSelectionChange"
      @sort-change="onSortChange"
      :data="tableData"
      height="450"
    >
      <el-table-column fixed type="selection" width="50"></el-table-column>
      <el-table-column
        fixed
        type="index"
        label="#"
        width="50"
      ></el-table-column>
      <el-table-column
        fixed
        prop="title"
        label="标题"
        width="300"
      ></el-table-column>
      <el-table-column prop="type" label="类型" width="200"></el-table-column>
      <el-table-column prop="author" label="作者" width="200"></el-table-column>
      <el-table-column
        prop="time"
        label="时间"
        sortable
        width="150"
      ></el-table-column>
      <el-table-column
        prop="date"
        label="日期"
        sortable
        width="200"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" size="small">查看</el-button>
          <el-button type="text" size="small" @click="onDelete(scope.row.id)"
            >删除</el-button
          >
          <el-button type="text" size="small" @click="upDataClk(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
    >
      <el-form
        :model="form"
        size="mini"
        style="padding: 0 5%"
        ref="form"
        label-width="100px"
        class="demo-form"
      >
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="resource">
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option
              v-for="(item, index) in classify"
              :key="index"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="name">
          <el-input :disabled="true" v-model="form.author"></el-input>
        </el-form-item>
        <el-form-item label="用时" prop="desc">
          <el-select v-model="form.time" placeholder="请选择用时">
            <el-option label="一分钟" value="1"></el-option>
            <el-option label="二分钟" value="2"></el-option>
            <el-option label="三分钟" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <mavonEditor style="max-height: 300px; overflow-y: scroll" v-model="form.content"></mavonEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addClk">{{ btnText }}</el-button>
          <el-button @click="cancelClk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
// import { mavonEditor } from "mavon-editor";
// import "mavon-editor/dist/css/index.css";
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      drawer: false,
      form: {
        author: "situ",
      },
      title: "新增",
      btnText: "立即创建",
    };
  },
  mounted() {
    this.getList();
    this.getClassify();
    this.addlist();
  },
  methods: {
    ...mapActions("msbd", [
      "getList",
      "getClassify",
      "addlist",
      "delList",
      "updateList",
    ]),
    // 删除全部
    deleteAll(id){
      console.log(id);
    },
    //确认添加提交时
    addClk() {
      this.drawer = false;
     if(this.form.id) {
       this.updateList(this.form)
     }else {
       this.addlist(this.form)
     }
      this.getList();

      this.form = {
        author: "situ",
      };
    },
    cancelClk() {
      this.drawer = false;
      //取消
    },
    //删除
    onDelete(t) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delList(t);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改
    upDataClk(v) {
      this.drawer = true;
      this.title = "修改";
      this.update = "修改";
      this.form = { ...v };
    },
    //增加
    addDataBtn() {
      this.drawer = true;
      this.title = "新增";
    },
    //抽屉关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleSelectionChange(id) {},
    //排序
    onSortChange(value) {},

    handleSizeChange(val) {
      console.log(val);
    },
    handleCurrentChange(val) {},
  },
  computed: {
    ...mapState("msbd", ["tableData", "classify"]),
  },
};
</script>

<style scoped>
</style>
